package cn.lxh.basic.controller;

import cn.lxh.basic.dto.RegisterDto;
import cn.lxh.basic.exception.BusinessException;
import cn.lxh.basic.service.IVerifyCodeService;
import cn.lxh.org.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{imageCodeKey}")
    /**
     * 接收请求路径中占位符
     */
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String key){
        try {
            String base64Str = verifyCodeService.imageCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * smsCode
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信验证码异常！"+e.getMessage());
        }
    }


}
